package jiaxa.com.qqlogin;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {

    private Button btnLogin;
    private EditText edtUserName;
    private EditText edtPassword;
    private CheckBox chkRemember;

    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new btnLoginClickListener());

        edtUserName = (EditText) findViewById(R.id.edtUserName);
        edtPassword = (EditText) findViewById(R.id.edtPassword);
        chkRemember = (CheckBox) findViewById(R.id.chkRemember);

        //        restoreMsg();
        //       fileSave();

        sp = this.getSharedPreferences("config", this.MODE_PRIVATE);
        restoreSP();
    }

    /*
    存储信息
    */
    private void fileSave() {
        File dataFile = Environment.getDataDirectory();
        File sdFile = Environment.getExternalStorageDirectory();
        long dataSize = dataFile.getTotalSpace();
        long sdSize = sdFile.getTotalSpace();
        //        Toast.makeText(this,"内部存储："+ Formatter.formatFileSize(this,dataSize)+"，外部存储："+Formatter.formatFileSize(this,sdSize),Toast.LENGTH_LONG).show();
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

        } else {
            Toast.makeText(MainActivity.this, "sd卡被拔出或不可用", Toast.LENGTH_SHORT).show();
        }
    }

    /*
    * 读取帐号和密码，显示到界面
    * */
    private void restoreMsg() {
        File file = new File(getFilesDir(), "info.txt");
        if (file.exists() && file.length() > 0) {
            try {
                FileInputStream fis = new FileInputStream(file);
                BufferedReader buffer = new BufferedReader(new InputStreamReader(fis));
                String msg = buffer.readLine();
                String[] info = msg.split("###");
                edtUserName.setText(info[0]);
                edtPassword.setText(info[1]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /*
    *
    * */
    private void restoreSP() {
        String username = sp.getString("username", "");
        String password = sp.getString("password", "");
        edtUserName.setText(username);
        edtPassword.setText(password);

    }

    //点击登录操作
    private class btnLoginClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            String username = edtUserName.getText().toString().trim();
            String password = edtPassword.getText().toString().trim();
            if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                Toast.makeText(MainActivity.this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            if (chkRemember.isChecked()) {
                saveUser(username, password);
                //                try {
                //                    File file = new File(getFilesDir(), "info.txt");
                //                    FileOutputStream fos = new FileOutputStream(file);
                //                    String msg = username + "###" + password;
                //                    fos.write(msg.getBytes());
                //                    fos.close();
                //                } catch (Exception e) {
                //                    e.printStackTrace();
                //                }
            }
            if ("000".equals(username) && "123456".equals(password)) {
                Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    void saveUser(String username, String password) {
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("username", username);
        edit.putString("password", password);
        edit.commit();
    }
}
